package com.baibei.ebec.follow.fragment;

import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.baibei.ebec.home.R;
import com.baibei.ebec.home.R2;
import com.baibei.module.BasicFragment;
import com.baibei.module.ViewUtil;
import com.blankj.utilcode.utils.LogUtils;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by tang on 2018/8/13.
 */

public class ProfitCountFragment extends BasicFragment {
    //    @BindView(R2.id.bar_chart)
//    BarChart barChart;
    Unbinder unbinder;
    @BindView(R2.id.tv_up1)
    TextView tvUp1;
    @BindView(R2.id.up1)
    View up1;
    @BindView(R2.id.tv_up2)
    TextView tvUp2;
    @BindView(R2.id.up2)
    View up2;
    @BindView(R2.id.tv_up3)
    TextView tvUp3;
    @BindView(R2.id.up3)
    View up3;
    @BindView(R2.id.tv_up4)
    TextView tvUp4;
    @BindView(R2.id.up4)
    View up4;
    @BindView(R2.id.tv_up5)
    TextView tvUp5;
    @BindView(R2.id.up5)
    View up5;
    @BindView(R2.id.down1)
    View down1;
    @BindView(R2.id.tv_down1)
    TextView tvDown1;
    @BindView(R2.id.down2)
    View down2;
    @BindView(R2.id.tv_down2)
    TextView tvDown2;
    @BindView(R2.id.down3)
    View down3;
    @BindView(R2.id.tv_down3)
    TextView tvDown3;
    @BindView(R2.id.down4)
    View down4;
    @BindView(R2.id.tv_down4)
    TextView tvDown4;
    @BindView(R2.id.down5)
    View down5;
    @BindView(R2.id.tv_down5)
    TextView tvDown5;
    @BindView(R2.id.tv_amount)
    TextView tvAmount;
    @BindView(R2.id.tv_rate)
    TextView tvRate;
    private ArrayList<Double> upArr;
    private ArrayList<Double> downArr;
    private String sumAmount;
    private String amountRate;
    private ArrayList<Double> detail;
    private View[] downViews;
    private View[] upViews;
    private TextView[] upTvs;
    private TextView[] downTvs;
    private double max;

    public static ProfitCountFragment newInstance(String sumAmount, String amountRate, ArrayList<String> upDetail,ArrayList<String> downDetail) {
        Bundle args = new Bundle();
        args.putString("sumAmount", sumAmount);
        args.putString("amountRate", amountRate);
        args.putStringArrayList("upDetail", upDetail);
        args.putStringArrayList("downDetail",downDetail);
        ProfitCountFragment fragment = new ProfitCountFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getLayoutId(Bundle bundle) {
        return R.layout.fragment_profit_count;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle args = getArguments();
        sumAmount = args.getString("sumAmount");
        amountRate = args.getString("amountRate");
        ArrayList<String> upDetail = args.getStringArrayList("upDetail");
        ArrayList<String> downDetail = args.getStringArrayList("downDetail");
        upArr = new ArrayList<>();
        downArr = new ArrayList<>();
        for (String s : upDetail){
            upArr.add(Double.parseDouble(s));
        }
        for (String s :downDetail){
            downArr.add(Double.parseDouble(s));
        }
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        tvAmount.setText(sumAmount+"元");
        tvRate.setText(amountRate+"%");

        upViews = new View[]{up1, up2, up3, up4, up5};
        upTvs = new TextView[]{tvUp1, tvUp2, tvUp3, tvUp4, tvUp5};

        downViews = new View[]{down1, down2, down3, down4, down5};
        downTvs = new TextView[]{tvDown1, tvDown2, tvDown3, tvDown4, tvDown5};
        for (double d : upArr){
            if (d> max){
                max = d;
            }
        }
        for (double d : downArr){
            if (d> max){
                max = d;
            }
        }
        if (max<100){
            max = 100;
        }
        setWin();
        setFail();
        for (int i = 0; i < upArr.size();i++){
            if (Double.doubleToLongBits(upArr.get(i)) == Double.doubleToLongBits(downArr.get(i))){
                upTvs[i].setText("0.0");
            }
        }
    }

    private void setFail() {
//        min/10*11
        double v = max / 10 * 11;
        for (int i = 0 ; i < downArr.size();i++){
            if (downArr.get(i)>0){
                ViewUtil.setHeight(downViews[i],dip2px(downArr.get(i)/v*92));
                downTvs[i].setText(downArr.get(i)+"");
            }
            if (downArr.get(i)<=0){
                downViews[i].setVisibility(View.INVISIBLE);
            }
        }
    }

    private void setWin() {
        double v = max / 10 * 11;
        for (int i = 0; i < upArr.size(); i++) {
            if (upArr.get(i) > 0) {
                ViewUtil.setHeight(upViews[i], dip2px(upArr.get(i) / v * 184));
                upTvs[i].setText(upArr.get(i) + "");
            }
            if (upArr.get(i) <= 0) {
                upViews[i].setVisibility(View.INVISIBLE);
            }
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    public static int dip2px(double dipValue) {
        final float scale = Resources.getSystem().getDisplayMetrics().density;
        if ((int)(dipValue*scale+0.5f) == 0){
            return 1;
        }
        return (int) (dipValue * scale + 0.5f);
    }
}
